package view;

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

import controller.CustomerController;
import controller.SpecialOrderController;

public class SpecialOrderTab extends JPanel implements ActionListener{

	private JButton findButton, createButton, updateButton,removeItemButton, clearButton,cancelOrderButton;
	private JLabel searchLabel, findLabel, customerInfoLabel, nameLabel, addressLabel, postalCodeLabel, cityLabel, phoneLabel, emailLabel, searchException, standardException, orders,statusLabel;
	private JTextField nameField, addressField, postalCodeField, cityField, phoneField, emailField,statusField;
	private static JTextField searchField;
	private JPanel row1, row2, row3, row4, row5, row6, row7, row8,row9;
	private JSeparator line;
	private JScrollPane scrollPane;
	private JTable table;
	private JCheckBox deliverCheckBox,receivedPaymentCheckBox;
	private SpecialOrderTableModel model;
	private SpecialOrderController specialOrderController = SpecialOrderController.getInstance();
	private CustomerController customerController = CustomerController.getInstance();
	
	public SpecialOrderTab(){
		
		setLayout(null);
		
		//Row 1
		row1 = new JPanel();
		row1.setBounds(0, 0, 600, 60);
		row1.setLayout(null);
		add(row1);
		searchLabel = new JLabel("Order ID");
		searchLabel.setBounds(15, 18, 60, 10);
		searchField = new JTextField(15);
		searchField.setBounds(90, 15, 150, 20);
		findButton = new JButton("Find");
		findButton.setBounds(250, 15, 80, 20);
		findButton.addActionListener(this);
		row1.add(searchLabel);
		row1.add(searchField);
		row1.add(findButton);
		//searchException label
		searchException = new JLabel();
		searchException.setBounds(15, 40, 400, 20);
		searchException.setForeground(Color.red);
		row1.add(searchException);
		
		//Row 2
		row2 = new JPanel();
		row2.setBounds(0, 60, 600, 35);
		row2.setLayout(null);
		add(row2);
		customerInfoLabel = new JLabel("Customer Info");
		customerInfoLabel.setBounds(120,0,80,10);
		row2.add(customerInfoLabel);
		phoneLabel = new JLabel("Phone/ID");
		phoneLabel.setBounds(15, 18, 80, 10);
		phoneField = new JTextField(15);
		phoneField.setBounds(90, 15, 150, 20);
		row2.add(phoneLabel);
		row2.add(phoneField);
		//standardException label
		standardException = new JLabel();
		standardException.setBounds(250, 18, 350, 20);
		standardException.setForeground(Color.red);
		row2.add(standardException);
		
		//Row 3
		row3 = new JPanel();
		row3.setBounds(0, 85, 600, 35);
		row3.setLayout(null);
		add(row3);
		nameLabel = new JLabel("Name");
		nameLabel.setBounds(15, 18, 60, 10);
		nameField = new JTextField(15);
		nameField.setBounds(90, 15, 150, 20);
		receivedPaymentCheckBox = new JCheckBox("Payment Recieved");
		receivedPaymentCheckBox.setBounds(250,10,180,20);
		//receivedPaymentCheckBox.addItemListener(ItemEvent);
		row3.add(receivedPaymentCheckBox);
		row3.add(nameLabel);
		row3.add(nameField);
		
		//Row 4
		row4 = new JPanel();
		row4.setBounds(0, 110, 600, 35);
		row4.setLayout(null);
		add(row4);
		addressLabel = new JLabel("Address");
		addressLabel.setBounds(15, 18, 60, 10);
		addressField = new JTextField(15);
		addressField.setBounds(90, 15, 150, 20);
		
		row4.add(addressLabel);
		row4.add(addressField);
		//deliverCheckBox = new JCheckBox("Deliver Order to customer");
		//deliverCheckBox.setBounds(250, 15, 180, 20);
		//row4.add(deliverCheckBox);
		
		//Row 5
		row5 = new JPanel();
		row5.setBounds(0, 135, 600, 35);
		row5.setLayout(null);
		add(row5);
		postalCodeLabel = new JLabel("Postal Code");
		postalCodeLabel.setBounds(15, 18, 80, 10);
		postalCodeField = new JTextField(15);
		postalCodeField.setBounds(90, 15, 150, 20);
		cityLabel = new JLabel("City");
		cityLabel.setBounds(250, 15, 60, 20);
		cityField = new JTextField(15);
		cityField.setBounds(290, 15, 150, 20);
		row5.add(postalCodeLabel);
		row5.add(postalCodeField);
		row5.add(cityLabel);
		row5.add(cityField);
		
		//Row 6
		row6 = new JPanel();
		row6.setBounds(0, 160, 600, 35);
		row6.setLayout(null);
		add(row6);
		emailLabel = new JLabel("Email");
		emailLabel.setBounds(15, 18, 80, 10);
		emailField = new JTextField(15);
		emailField.setBounds(90, 15, 150, 20);
		row6.add(emailLabel);
		row6.add(emailField);
		
		//Row 7
		row7 = new JPanel();
		row7.setBounds(0, 195, 600, 80);
		row7.setLayout(null);
		add(row7);
		
		createButton = new JButton("Create Order");
		createButton.setBounds(15, 20, 120, 20);
		createButton.addActionListener(this);
		
		updateButton = new JButton("Update Order");
		updateButton.setBounds(140, 20, 120, 20);
		updateButton.addActionListener(this);
		
		removeItemButton = new JButton("Remove Item");
		removeItemButton.setBounds(265, 20, 120, 20);
		removeItemButton.addActionListener(this);
		
		clearButton = new JButton("Clear Order");
		clearButton.setBounds(390, 20, 120, 20);
		clearButton.addActionListener(this);
		
		row7.add(createButton);
		row7.add(updateButton);
		row7.add(removeItemButton);
		row7.add(clearButton);
		
		line = new JSeparator();
		line.setBounds(10,60, 560, 30);
		row7.add(line);
				
		// row 8 
		row8 = new JPanel();
		row8.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.black), "Items"));
		row8.setBounds(10, 280, 560, 180);
		row8.setLayout(new GridLayout(1,0));
		add(row8);

		// insert orders into a table inside a scroll pane
		// temp
		ArrayList<Object[]> altest = new ArrayList<Object[]>();
		altest.add(new Object[]{1, "test1", 1, 2.0});
		altest.add(new Object[]{2, "test1", 2, 4.0});
		altest.add(new Object[]{3, "test1", 3, 6.0});
		altest.add(new Object[]{4, "test1", 4, 8.0});
		// temp slut

		model = new SpecialOrderTableModel(new ArrayList<Object[]>(), new String[]{"Item Id", "Item name", "Amount", "Price"});
		table = new JTable(model);		
		scrollPane = new JScrollPane(table);
		table.setFillsViewportHeight(true);
		row8.add(scrollPane);


		// row 9
		row9 = new JPanel();
		row9.setLayout(null);
		row9.setBounds(0, 470, 600, 100);
		add(row9);
		statusLabel = new JLabel("Order Status");
		statusLabel.setBounds(20, 0, 150, 20);
		statusField = new JTextField(15);
		statusField.setBounds(20, 25, 150, 20);
		row9.add(statusLabel);
		row9.add(statusField);
		cancelOrderButton = new JButton("Cancel Order");
		cancelOrderButton.setBounds(400,25,120,20);
		row9.add(cancelOrderButton);
	}
		
	public void itemStateChanged(ItemEvent e) {
	    Object source = e.getItemSelectable();
	    if (source == receivedPaymentCheckBox) {    
	    	System.out.println("box checked");
	    }
	    if (e.getStateChange() == ItemEvent.DESELECTED) {
	    	System.out.println("box unchecked");
	    }
	    System.out.println("Der sket vist noget");
	}
	
		@Override
		public void actionPerformed(ActionEvent e) {
			if(e.getSource() == createButton){
				System.out.println("Creating Order.");
				try{
					//clearException();
					createOrder();
				} catch(Exception ex) {
					//standardException();
				}
			}
			if(e.getSource() == updateButton){
				System.out.println("Saving.");
				try{
					//clearException();
					updateOrder();
				} catch(Exception ex) {
					//standardException();
				}
			}
			if(e.getSource() == findButton){
				System.out.println("Searching.");
				try{
					//clearException();
					findOrder();
				} catch(Exception ex) {
					//searchException();
				}
			}
			if(e.getSource() == clearButton){
				System.out.println("Clearing.");
				//clearFields();
			}
		}
		
		private void createOrder() {
			//SpecialOrderController.createSpecialOrder()
		}
		
		private void findOrder() {
			int orderNumber = Integer.parseInt(searchField.getText());
			int customerNumberfromOrderId = specialOrderController.getCustomerId(orderNumber) ;
			nameField.setText(customerController.getName((customerNumberfromOrderId)));
			addressField.setText(customerController.getAdress(customerNumberfromOrderId));
			postalCodeField.setText(String.valueOf(customerController.getPostalCode(customerNumberfromOrderId)));
			cityField.setText(customerController.getCity(customerNumberfromOrderId));
			phoneField.setText(String.valueOf(customerController.getPhone(customerNumberfromOrderId)));
			emailField.setText(customerController.getEmail(customerNumberfromOrderId));
			statusField.setText(specialOrderController.getStatus(orderNumber));
			receivedPaymentCheckBox.setSelected(specialOrderController.isPaid(orderNumber));
		}
			
		private ArrayList<Object[]> getItems(int orderId){
			return specialOrderController.getItems(orderId);
		}
		
		public void updateTable(){
			row8.remove(scrollPane);
			table = new JTable(new OrderTableModel(getItems(Integer.parseInt(searchField.getText())), new String[]{"Item Id", "Item name", "Amount", "Price"}));
			scrollPane = new JScrollPane(table);
			table.setFillsViewportHeight(true);
			row8.add(scrollPane);
		}
		
		private void updateOrder() {
			specialOrderController.setPaid(Integer.parseInt(searchField.getText()), receivedPaymentCheckBox.isSelected());
		}

		private void clearFields() {
			searchField.setText(null);
			nameField.setText(null); 
			addressField.setText(null);
			postalCodeField.setText(null);
			cityField.setText(null);
			phoneField.setText(null);
			emailField.setText(null);
			receivedPaymentCheckBox.setSelected(false);
			clearException();
		}
		
		private void searchException(){
			searchException.setText("Search Error - Try Again!");
		}
		
		private void standardException(){
			standardException.setText("Error! - Please Check The Information.");
		}
		
		private void clearException(){
			searchException.setText(null);
			standardException.setText(null);
		}
}
